#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "character/crune.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    pCharSelectDialog(NULL),pAboutDialog(NULL)
{
    ui->setupUi(this);
    pProgressBar = new QProgressBar(this);
    pProgressBar->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    ui->statusBar->insertPermanentWidget(0,pProgressBar);
    pProgressBar->setMaximumSize(150,10);

    initEditPage();

    abilityHandler = new HBDataHandler("data/abilities.hbd",Diablo3::ABILITIES
                                       ,Diablo3::DOCTYPEString(Diablo3::ABILITIES));

    abilityHandler->load();
    qDebug(abilityHandler->getDoc().toString().toStdString().c_str());
    setMode(CHARACTERMODE);
}

MainWindow::~MainWindow()
{
    delete pCharSelectDialog;
    delete pAboutDialog;
    delete pProgressBar;
    delete ui;
}

void MainWindow::on_actionNewCharacter_triggered()
{
    if(pCharSelectDialog==NULL)
        pCharSelectDialog = new CNewCharacterDialog(this);
    pCharSelectDialog->show();
}

void MainWindow::on_actionExit_triggered()
{
    this->close();
}

void MainWindow::setMode(EMODE newMode){
    setStatusBarText(QApplication::translate("MainWindow","Swap to ")+QApplication::translate("MainWindow",getModeText(newMode)),1000);
    activeMode = newMode;
    switch(activeMode){
    case CHARACTERMODE:
        ui->actionCharacterMode->setEnabled(false);
        ui->actionTraitMode->setEnabled(true);
        ui->actionSkillMode->setEnabled(true);
        ui->actionEditMode->setEnabled(true);
        ui->actionEditMode_AddAbility->setVisible(false);
        break;
    case TRAITMODE:
        ui->actionCharacterMode->setEnabled(true);
        ui->actionTraitMode->setEnabled(false);
        ui->actionSkillMode->setEnabled(true);
        ui->actionEditMode->setEnabled(true);
        ui->actionEditMode_AddAbility->setVisible(false);
        break;
    case SKILLMODE:
        ui->actionCharacterMode->setEnabled(true);
        ui->actionTraitMode->setEnabled(true);
        ui->actionSkillMode->setEnabled(false);
        ui->actionEditMode->setEnabled(true);
        ui->actionEditMode_AddAbility->setVisible(false);
        break;
    case EDITMODE:
        ui->actionCharacterMode->setEnabled(true);
        ui->actionTraitMode->setEnabled(true);
        ui->actionSkillMode->setEnabled(true);
        ui->actionEditMode->setEnabled(false);
        ui->actionEditMode_AddAbility->setVisible(true);
        break;
    }
    ui->pageWidget->setCurrentIndex((int)activeMode);
}

void MainWindow::on_actionCharacterMode_triggered()
{
    setMode(CHARACTERMODE);
}

void MainWindow::on_actionTraitMode_triggered()
{
    setMode(TRAITMODE);
}

void MainWindow::on_actionSkillMode_triggered()
{
    setMode(SKILLMODE);
}

void MainWindow::on_actionEditMode_triggered()
{
    setMode(EDITMODE);

}

void MainWindow::on_actionAbout_triggered()
{
    if(pAboutDialog==NULL)
        pAboutDialog = new CAboutDialog(this);
    pAboutDialog->show();
}

void MainWindow::on_actionHomepage_triggered()
{
    QDesktopServices::openUrl(QUrl("http://code.google.com/p/diablo-3-planer/"));
}

void MainWindow::on_statusBar_messageChanged(QString){
    if(ui->statusBar->currentMessage().length() == 0)
        ui->statusBar->showMessage(QApplication::translate("MainWindow",getModeText()));
}

const char* MainWindow::getModeText(EMODE mode)
{
    switch(mode){
    case CHARACTERMODE: return ("Character Page");
    case TRAITMODE:     return ("Trait Page");
    case SKILLMODE:     return ("Skill Tree");
    case EDITMODE:      return ("Edit Mode");
    }
    return ("Error");
}

const char* MainWindow::getModeText()
{
    return getModeText(activeMode);
}

void MainWindow::setStatusBarText(QString str,int d){
    ui->statusBar->showMessage(getModeText() + QString(" :: ") +str
                               ,d);
}

void MainWindow::on_actionLoadCharacter_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Load Character"),
                                                    QString(),
                                                    tr("Horadrim Book Character File (*.hbc)"));
    qDebug(fileName.toStdString().c_str());
}

void MainWindow::initEditPage(){
//  class text
    const char** p = Diablo3::DEFAULTCLASSNAMES;
    for(int i = 0;i<Diablo3::MAXCLASSES;i++,p++)
        ui->editClassBox->addItem(QApplication::translate("Diablo3",*p));
    ui->editClassBox->adjustSize();
//  rune text
    p = Diablo3::DEFAULTRUNENAMES;
    for(int i=0;i<Diablo3::MAXRUNES;i++,p++){
        ui->editRuneForDescBox->addItem(QApplication::translate("Diablo3",*p));
    }

    for(int i=0;i<ui->editBrowser->topLevelItemCount();i++){
        if(tr("Undefined") == ui->editBrowser->topLevelItem(i)->text(0)){
            undefinedHeader = ui->editBrowser->topLevelItem(i);
            continue;
        }
        if(tr("Skills") == ui->editBrowser->topLevelItem(i)->text(0)){
            skillHeader = ui->editBrowser->topLevelItem(i);
            continue;
        }
        if(tr("Traits") == ui->editBrowser->topLevelItem(i)->text(0)){
            traitHeader = ui->editBrowser->topLevelItem(i);
            continue;
        }
    }
    qDebug(undefinedHeader->text(0).toStdString().c_str());
    qDebug(skillHeader->text(0).toStdString().c_str());
    qDebug(traitHeader->text(0).toStdString().c_str());
}

void MainWindow::on_actionEditMode_AddAbility_triggered()
{
    CEditAbilityItem newAbility(tr("newAbility"));
    childs.push_front(newAbility);
    undefinedHeader->addChild(&childs.front());
}

void MainWindow::on_typeBox_currentIndexChanged(const QString &arg1)
{

}

void MainWindow::on_editBrowser_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    QTreeWidgetItem *par = current->parent();
    if(par){//non-header ; actual skill
        //save data from previous if changed
        //update view to current

    }
}


